package com.applet.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.applet.annotation.SystemServerLog;
import com.applet.entity.request.VerificationCodeSmsRequest;
import com.applet.entity.response.VerificationCodeSmsResponse;
import com.applet.enums.ResultEnums;
import com.applet.model.SmsLog;
import com.applet.service.SmsService;
import com.applet.utils.AppletResult;
import com.applet.utils.ResultUtil;
import com.applet.utils.common.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsServiceImpl extends BaseServiceImpl implements SmsService{

    private static final Logger LOGGER= LoggerFactory.getLogger(SmsServiceImpl.class);

    private static final String verificationTemplateCode = "SMS_138885019";

    private static final String signName = "丁骏";

    private static final String SmsKey = "VerificationCodeSmsKey_";

    @SystemServerLog(funcionExplain = "发送短信信息")
    @Override
    public AppletResult sendVerificationCodeSms(VerificationCodeSmsRequest sendSmsRequest){

        if(redisUtil.getValueObj(SmsKey + sendSmsRequest.getPhone()) == null){
            //发短信
            try{
                SendSmsResponse response = smsUtils.sendSms(sendSmsRequest.getPhone(),
                        signName,verificationTemplateCode,sendSmsRequest.getCode());
                System.out.println("短信接口返回的数据----------------");
                System.out.println("Code=" + response.getCode());
                System.out.println("Message=" + response.getMessage());
                System.out.println("RequestId=" + response.getRequestId());
                System.out.println("BizId=" + response.getBizId());

                SmsLog smsLog = new SmsLog();
                smsLog.setId(UuidUtil.getUuid());
                smsLog.setPhone(sendSmsRequest.getPhone());
                smsLog.setSmsType(0);
                smsLog.setVerificationCode(sendSmsRequest.getCode());
                smsLog.setCreateDate(new Date());
                smsLog.setRequestId(response.getRequestId());
                smsLog.setRemarks(response.getMessage());
                smsLog.setBizId(response.getBizId());
                if("OK".equals(response.getCode())){
                    smsLog.setSmsState(1);
                }else{
                    smsLog.setSmsState(0);
                }
                smsLogMapper.insertSelective(smsLog);

                VerificationCodeSmsResponse verificationCodeSmsResponse = new VerificationCodeSmsResponse();
                verificationCodeSmsResponse.setMessage(response.getMessage());

                if("OK".equals(response.getCode())){
                    redisUtil.setObjAndExpire(SmsKey + sendSmsRequest.getPhone(),sendSmsRequest.getCode(),5*60);
                    return ResultUtil.success(ResultEnums.RETURN_SUCCESS,verificationCodeSmsResponse);
                }else{
                    return ResultUtil.error(ResultEnums.ERROR_SMS_SEND);
                }

            }catch (Exception e){
                LOGGER.error(e.getMessage());
                return ResultUtil.error(ResultEnums.SERVER_ERROR);
            }
        }else{
            return ResultUtil.error(ResultEnums.ERROR_SMS_REPEAT);
        }

    }
}
